public class HashBuck {

    //内部类，表示结点
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    //array数组表示使用的哈希表，存放的是单链表的头节点
    public Node[] array = new Node[10];
    //哈希表的实际使用大小
    public int usedSize;
    //指定哈希桶的α大小
    public static final double DEFAULT_LOAD_FACTOR = 0.75f;

    public void push(int key, int val){
        //计算哈希桶的索引
        int index = key % array.length;
        Node cur = array[index];

        //查找表中是否存在相同的key对应的结点，如果有，更新val
        while(cur != null){
            if(cur.key == key){
                cur.val = val;
                return;
            }
            cur = cur.next;
        }

        //若没有在表中找到key对应的结点，则创建一个新的结点头插到该下标
        Node node = new Node(key, val);
        node.next = array[index];
        array[index] = node;

        //每次插入更新其值
        usedSize++;
        //如果超过了负载因子，要对数组进行扩容（二倍）
        if(doLoadFactor() >= DEFAULT_LOAD_FACTOR){

        }
    }
    private double doLoadFactor(){
        return usedSize * 1.0 / array.length;
    }

    //由于数组长度变化，哈希表索引也改变，要重新遍历哈希表中所有结点，重新进行填放
    private void resize(){
        Node[] newArray = new Node[2*array.length];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while(cur != null){
                int newIndex = cur.key % newArray.length;
                Node curN = cur.next;
                cur.next = newArray[newIndex];
                newArray[newIndex] = cur;
                cur = curN;
            }
        }
        array = newArray;
    }

    public int getVal(int key){
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null){
            if(cur.key == key){
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }
}
